package com.hotelcrs.hotelcrsapi.controller;

import com.hotelcrs.hotelcrsapi.pojo.ResponseMessage;
import com.hotelcrs.hotelcrsapi.pojo.User;
import com.hotelcrs.hotelcrsapi.pojo.dto.UserDto;
import com.hotelcrs.hotelcrsapi.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController //接口方法返回对象 转换成json文本
@RequestMapping("/user")  //localhost:8088/user/**
public class UserController {

    @Autowired
    private IUserService userService;
    //增加
    @PostMapping  //URL:localhost:8088/user  method:post
    public ResponseMessage<User> add(@Validated @RequestBody UserDto user)
    {
        User userNew=userService.add(user);
        return ResponseMessage.success(userNew);
    }
    //查询
    @GetMapping("/{userId}")//URL:localhost:8088/user/userId  method:get
    public ResponseMessage get(@PathVariable Integer userId)
    {
        User user=userService.getUser(userId);
        return ResponseMessage.success(user);
    }
    //修改
    @PutMapping//URL:localhost:8088/user/userId  method:put
    public ResponseMessage edit(@Validated @RequestBody UserDto user)
    {
        User userNew=userService.edit(user);
        return ResponseMessage.success(userNew);
    }

    //删除用户
    @DeleteMapping("/{userId}")//URL:localhost:8088/user/userId  method:delete
    public ResponseMessage delete(@PathVariable Integer userId)
    {
        userService.delete(userId);
        return ResponseMessage.success();
    }
}
